package com.hex.liyue.lookingdatemodule.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hex.liyue.R;
import com.hex.liyue.commonmodule.activity.BaseActivity;
import com.hex.liyue.commonmodule.utils.L;
import com.hex.liyue.commonmodule.utils.SexUtils;
import com.hex.liyue.commonmodule.utils.StatusBarCompatUtils;
import com.hex.liyue.commonmodule.view.scrollbanner.BannerViewPager;
import com.hex.liyue.commonmodule.view.scrollbanner.ViewFactory;
import com.hex.liyue.lookingdatemodule.presenter.IPersonDetailsView;
import com.hex.liyue.lookingdatemodule.presenter.PersonDetailsPresenter;
import com.hex.liyue.usermodule.bean.MyMessageResultBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by WJ on 2016/6/12.
 * 个人信息详情的界面
 */
public class PersonDetailsActivity extends BaseActivity implements IPersonDetailsView {

    @Bind(R.id.iv_toobar_back)
    ImageView ivToobarBack;
    @Bind(R.id.tv_toobar_title)
    TextView tvToobarTitle;
    @Bind(R.id.tv_toobar_right)
    TextView tvToobarRight;
    @Bind(R.id.bn_person_banner)
    BannerViewPager bnPersonBanner;

    @Bind(R.id.tv_username)
    TextView tvUsername;
    @Bind(R.id.tv_sex_age)
    TextView tvSexAge;
    @Bind(R.id.tv_star_sign)
    TextView tvStarSign;
    @Bind(R.id.tv_signature)
    TextView tvSignature;
    @Bind(R.id.iv_details)
    ImageView ivDetails;

    private List<ImageView> views;//图片组件
    private List<String> infos;//图片URL地址
    private String uid;//用户ID
    private PersonDetailsPresenter personDetailsPresenter;


    @Override
    protected void bindPresenter() {
        personDetailsPresenter = new PersonDetailsPresenter(this);
    }

    @Override
    protected void initData(Bundle extras) {
        uid = getIntent().getStringExtra("uid");
        personDetailsPresenter.getProfile(uid);
        // 在加载数据前设置是否循环
        infos = new ArrayList<>();
        views = new ArrayList<>();
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_person_details;
    }

    @Override
    protected void initToolbar() {
        StatusBarCompatUtils.compat(this);
        ivToobarBack.setVisibility(View.VISIBLE);
        tvToobarRight.setVisibility(View.VISIBLE);
        tvToobarRight.setText("举报");
    }

    @OnClick({R.id.iv_toobar_back, R.id.tv_toobar_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_toobar_back:
                onBackPressed();
                break;
            case R.id.tv_toobar_right://举报
                Intent intent = new Intent(PersonDetailsActivity.this, AboutActivity.class);
                intent.putExtra("uid", uid);
                intent.putExtra("look", 1 + "");
                startActivity(intent);
                break;
        }
    }

    @Override
    public void showLod(String msg) {
        showLoad(this, msg);
    }

    @Override
    public void closeLod() {
        closeLoad();
    }

    @Override
    public void showT(String msg) {
        showToast(this, msg);
    }

    @Override
    public void setPersonDetails(MyMessageResultBean myMessageResultBean) {
        tvToobarTitle.setText(myMessageResultBean.data.nickname);
        tvUsername.setText(myMessageResultBean.data.nickname);
        tvSexAge.setText(SexUtils.getSex(myMessageResultBean.data.sex) + "   保密");
//        tvSexAge.setText(SexUtils.getSex(myMessageResultBean.data.sex) + " " + DateUtils.getAge(myMessageResultBean.data.birthday*1000)+"");
        if (!TextUtils.isEmpty(myMessageResultBean.data.star_sign)) {
            tvSignature.setText(myMessageResultBean.data.signature);
        }

        if (!"".equals(myMessageResultBean.data.star_sign)) {
            tvStarSign.setText(myMessageResultBean.data.star_sign);
        } else {
            tvStarSign.setText("呀！不知道ta什么星座");
        }

        if (myMessageResultBean.data.urls.size() > 0) {
            ivDetails.setVisibility(View.GONE);
            for (MyMessageResultBean.DataBean.UrlsBean urlsBean : myMessageResultBean.data.urls) {
                infos.add(urlsBean.url);
            }
            L.d("WJ", "size=" + infos.toString());
            if (infos.size() >= 1) {
                // 将第一个ImageView添加进来
                views.add(ViewFactory.getImageView(this, infos.get(0)));
                if (infos.size() > 1) {
                    // 将最后一个ImageView添加进来
                    views.add(ViewFactory.getImageView(this, infos.get(infos.size() - 1)));
                    for (int i = 0; i < infos.size(); i++) {
                        views.add(ViewFactory.getImageView(this, infos.get(i)));
                    }
                }
                bnPersonBanner.setData(views, infos, null);
                //开始轮播,必须调用这个方法,不然不会自动轮播
                if (views.size() == 1) {
                    bnPersonBanner.setWheel(false);
                } else {
                    bnPersonBanner.setWheel(true);
                }
                // 设置轮播时间，默认3000ms
                bnPersonBanner.setScrollTime(2000);
                //设置圆点指示图标组居中显示，默认靠右
                bnPersonBanner.setIndicatorCenter();
            }

        } else {
            ivDetails.setVisibility(View.VISIBLE);
            bnPersonBanner.setVisibility(View.GONE);
            Glide.with(this).load(R.drawable.default_error).into(ivDetails);
        }
    }


}
